package com.wenc.psi.utils;

import com.wenc.psi.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.FutureTask;

public class Select {
    class people{
        private String name;
    }
    /**登录方法
     * @param userid 用户账号
     * @param password 用户密码
     * */
    public static boolean login(String userid, String password) {
        // 成功标志
        Boolean flag = false;
        // 为空直接返回
        if (userid == null || password == null) {
            return false;
        }
        FutureTask<Boolean> futureTask = new FutureTask(()->{
            //连接数据库判断是否登录成功
            Connection conn = null;
            PreparedStatement pstmt = null;
            ResultSet rs =null;
            try {
                //1.获取连接
                conn = JDBCUtils.getConnection();
                System.out.println("获取连接成功");
                //2.定义sql
                String sql = "select * from Employee where userid = ? and password = ? ";
                //3.获取执行sql对象
                pstmt = conn.prepareStatement(sql);
                //4.给?赋值
                pstmt.setString(1,userid);
                pstmt.setString(2,password);
                //5.执行查询,不需要传递sql
                rs = pstmt.executeQuery();
                //6.判断
                if (rs.next()) return true;
                else return false;
            } catch (SQLException e) {
                System.out.println("sql执行失败");
                e.printStackTrace();
                return false;
            } finally {
                JDBCUtils.close(rs, pstmt, conn);
            }
        });
        // 开启线程执行
        new Thread(futureTask).start();
        // 拿到返回值
        try {
            flag = futureTask.get();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
}
